"""
需求：获取网易云音乐中任意歌单或歌曲的评论
nodejs安装第三方库crypto-js
npm install crypto-js -g/-s
"""
import requests
import jsonpath
import execjs
import json

url = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='
# 1663743825590_1663763845771_tUQEsWd7R+vKqFiQIq1ZW8rav2vgE09FBnQG61Mc28otayg4Ov306i5GvlnhOerDwoqkxeTcHGNq9WRkdfI0Y2mkA1pG8/YRTtysB43WKlHBHQs2Qv3N+2ywTL7uUb02cScf1LXwvDRZkpGq1lDgx7VPONryPxnCd5o5RaCAgrxTOGgFDnOKDI6BgdGLrSJnr/HO2N/jVozgqmNmcViBadvFZ6zSdB8EfDuMJWpiL5zWWTSZ9/tvmKq4ov3MfyYFbsM4g3sx7O9ReCyOotUmmrTged6iDQ3MlPBLf7aEV3k3qFNLrJD/wqJSo/FBeuzVlMfzHngpwEuBbEOqbw9xgA==
# 1663743825590_1663764439969_tUQEsWd7R+vKqFiQIq1ZW8rav2vgE09FBnQG61Mc28otayg4Ov306i5GvlnhOerDwoqkxeTcHGNq9WRkdfI0Y2mkA1pG8/YRTtysB43WKlHBHQs2Qv3N+2ywTL7uUb02cScf1LXwvDRZkpGq1lDgx7VPONryPxnCd5o5RaCAgrxTOGgFDnOKDI6BgdGLrSJnr/HO2N/jVozgqmNmcViBaQA7wgR4Fu7mKQ2gSZNiv7wlv113gjRuB51/mEVrwypH+6cWxnnGi+eJYAY4Nfu/1iZ+KS+CfIZfO1K+esHIWQ4vAuD4u3hmH0+QJYvJDX4TeBbtLMbie5ukllNJ4pixnVMS8EHzbAIMyv+W18qr7KM=
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
    'referer': '',
}
r = {"rid": "A_PL_0_26467411", "threadId": "A_PL_0_26467411", "pageNo": "1", "pageSize": "20", "cursor": "-1",
     "offset": "0", "orderType": "1", "csrf_token": ""}
with open('jscode.js', 'r', encoding='utf-8')as f:
    js_code = f.read()
# 2. 实例化一个js代码可执行对象
js_obj = execjs.compile(js_code)   # 加载js代码
# 3. 执行js代码，调用入口函数
sign = js_obj.call('run', json.dumps(r))    # 第一个参数表示要调用的js代码中的函数，只用传入函数名
print(sign)
data = {
    'params': sign.get('encText'),
    'encSecKey': sign.get('encSecKey')
}
response = requests.post(url, headers=headers, data=data)
res = response.json()
print(res)
